var fs = require('fs'); // get the fs module
var stream = require('stream')
var Transform = stream.Transform;
var readline = require('readline');
var iconv = require('iconv-lite')

var readStream = fs.createReadStream('/home/looop/detail.log');

function GbkTransformer(options) {
	if (!(this instanceof GbkTransformer))
		return new GbkTransformer(options);

	Transform.call(this, options);
}

GbkTransformer.prototype = Object.create(Transform.prototype, {
	constructor : {
		value : GbkTransformer
	}
});

GbkTransformer.prototype._transform = function(chunk, encoding, done) {
	var decodechunk = iconv.decode(chunk, 'gbk');
//	console.log(iconv.encode(decodechunk, 'utf-8') + '')
	this.push(decodechunk);
	done();
}

var transformer = new GbkTransformer();
readStream.pipe(transformer);

var lineReader = readline.createInterface({
	input : transformer,
	output : new stream.Writable()
});
lineReader.on('line', function(line){
	console.log(line)
})


var util = require('util');
require('http').createServer(function(req, res) {
	var rs = fs.createReadStream('/home/looop/detail.log');
	util.pump(rs, res, function() {
		res.end();
	});
}).listen(1337, "127.0.0.1");
